package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.common.db.dao.ZjfGoodsActivityRecordDAO;
import com.ljkj.common.db.domain.ZjfGoodsActivityRecord;
import com.ljkj.common.db.domain.ZjfUser;

public class ActivityRecordService {
	
	private ZjfGoodsActivityRecordDAO zjfGoodsActivityRecordDAO;
	private ZjfUserService zjfUserService;
	private TLogService tLogService;

	/**
	 * 获取权限列表
	 * @param params 权限名称关键字
	 * @param currentPage 当前页
	 * @param pageSize 每页条数
	 * @param sortField 排序字段
	 * @param sortOrder 排序方式
	 * @return
	 */
	public String getActivityRecordByProperties(String params,String activityId,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfGoodsActivityRecord.class};
		String[] propertyNames= {"activityId"};
		Object[] values= {activityId};
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfGoodsActivityRecordDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfGoodsActivityRecordDAO.setPageMaxCount(pageSize);
		List<ZjfGoodsActivityRecord> zjfGoodsActivityRecords = this.zjfGoodsActivityRecordDAO.findByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfGoodsActivityRecord zjfGoodsActivityRecord : zjfGoodsActivityRecords){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			String openId = zjfGoodsActivityRecord.getOpenId();
			entry_map.put("openId",openId);
			if(openId != null && !"".equals(openId)){
				ZjfUser zjfUser = this.zjfUserService.getZjfUserByOpenId(openId);
				if(zjfUser != null){
					entry_map.put("userNickName",zjfUser.getUserName());
				}
			}
			entry_map.put("activityId",zjfGoodsActivityRecord.getActivityId());
			entry_map.put("join_time",zjfGoodsActivityRecord.getJoin_time());
			if(zjfGoodsActivityRecord.getJoin_time() == null || "".equals(zjfGoodsActivityRecord.getJoin_time())){
				entry_map.put("join_time",zjfGoodsActivityRecord.getCreateTime());
			}
			entry_map.put("money",zjfGoodsActivityRecord.getMoney());
			entry_map.put("order_num",zjfGoodsActivityRecord.getOrder_num());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	


	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}
	public void setZjfGoodsActivityRecordDAO(ZjfGoodsActivityRecordDAO zjfGoodsActivityRecordDAO) {
		this.zjfGoodsActivityRecordDAO = zjfGoodsActivityRecordDAO;
	}
	public void setZjfUserService(ZjfUserService zjfUserService) {
		this.zjfUserService = zjfUserService;
	}

}
